package com.food.platformmanagement.adapter.outbound;

import com.food.platformmanagement.domain.User;
import com.food.platformmanagement.domain.UserId;
import com.food.platformmanagement.port.outbound.UserRepository;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

/**
 * @author 24270
 */
public interface UserJpaRepository extends UserRepository, JpaRepository<User, UserId> {

    @Override
    default void addPlatformUser(User user) {
        save(user);
    }

    @Override
    default void deletePlatformUser(String id) {
        deleteById(UserId.of(id));
    }

    @Override
    default List<User> getAllPlatformUser() {
        return findAll();
    }

    /**
     * 查询指定用户，如果用户不存在则返回null
     * @param id
     * @return User or null
     */
    @Override
    default User getThePlatformUser(String id) {
        if(findById(UserId.of(id)).isPresent()) {
            return findById(UserId.of(id)).get();
        }
        return null;
    }

    @Override
    default void chagePlatformUser(User user) {
        save(user);
    }

    @Override
    default void addAuditor(User auditor) {
        save(auditor);
    }

    @Override
    default void deleteAuditor(String id) {
        deleteById(UserId.of(id));
    }

    @Override
    default List<User> getAllAuditor() {
        return findAll();
    }

    @Override
    default User getTheAuditor(String id) {
        if(findById(UserId.of(id)).isPresent()) {
            return findById(UserId.of(id)).get();
        }
        return null;
    }
}
